﻿<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl"	exclude-result-prefixes="php">
	
	<xsl:include href="utility.xsl" />
  <xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
  
  <xsl:variable name="TIMESTAMP" select="/response/@timestamp" />
  <xsl:variable name="USER"><xsl:value-of select="/response/@user" /></xsl:variable>
	<xsl:variable name="USER_IMAGE"><xsl:value-of select="/response/@user_image" /></xsl:variable>
	<xsl:variable name="CONTEXT_TYPE"><xsl:value-of select="/response/@context_type" /></xsl:variable>
	<xsl:variable name="CONTEXT_NAME"><xsl:value-of select="/response/@context_name" /></xsl:variable>
	<xsl:variable name="ANNOUNCE"><xsl:value-of select="/response/@unspoil" /></xsl:variable>
	<xsl:variable name="DOMAIN">http://melative.com</xsl:variable>
	<xsl:variable name="METHOD"><xsl:value-of select="/response/@op" /></xsl:variable>
	
	<xsl:template match="/">
    <div class="m-ev">
		  <xsl:apply-templates select="response" />
		</div>
	</xsl:template>
	
  <xsl:template match="response">
    <xsl:if test="event">
      <xsl:choose>
        <xsl:when test="$METHOD = 'get.context.activity'">
          <xsl:apply-templates select="event" mode="context" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="event" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>
  
  <xsl:template name="context-image">
    <xsl:param name="size" />
    <xsl:element name="a">
      <xsl:attribute name="rel">melative</xsl:attribute>
      <xsl:attribute name="href"><xsl:value-of select="$DOMAIN" />/<xsl:if test="context/@type != 'user'"><xsl:value-of select="context/@type" />/</xsl:if><xsl:value-of select="translate(context,' ','+')" /></xsl:attribute>
      <xsl:element name="img">
        <xsl:attribute name="class">cI</xsl:attribute>
        <xsl:attribute name="src">http://images.melative.com<xsl:value-of select="context/@image" />_<xsl:value-of select="$size" />.jpg</xsl:attribute>
        <xsl:attribute name="alt">_</xsl:attribute>
      </xsl:element>
    </xsl:element>
  </xsl:template>
  
  <xsl:template name="user-image">
    <xsl:param name="size" />
    <xsl:element name="a">
      <xsl:attribute name="href"><xsl:value-of select="$DOMAIN" />/<xsl:value-of select="@user" /></xsl:attribute>
      <xsl:element name="img">
        <xsl:attribute name="src">http://images.melative.com<xsl:value-of select="@image" />_<xsl:value-of select="$size" />.jpg</xsl:attribute>
        <xsl:attribute name="alt">_</xsl:attribute>
      </xsl:element>
    </xsl:element>
  </xsl:template>
  
  <xsl:template match="event">
    <xsl:element name="div">
    <xsl:attribute name="class">E <xsl:if test="action"> <xsl:value-of select="action/@type" /></xsl:if><xsl:if test="@thread = 1"> thread</xsl:if> e-<xsl:value-of select="@id" /></xsl:attribute>

      <xsl:if test="not(@thread) and context">
      <div class="L">
        <xsl:call-template name="context-image"><xsl:with-param name="size" select="string(64)" /></xsl:call-template>
      </div>
      </xsl:if>
      
      <div class="C">
      <xsl:if test="context and action and action/@type != 'reply'">
        <xsl:call-template name="action-context" />
		  </xsl:if>
		  <xsl:if test="message != ''">
        <p class="M"><xsl:value-of select="php:functionString('melative_link_message',message)" disable-output-escaping="yes" /></p>
      </xsl:if>
      </div>
      
      <div class="meta">
			<xsl:choose>
			  <xsl:when test="context and action and action/@type = 'reply'"><xsl:call-template name="link-context-reply" /></xsl:when>
			  <xsl:when test="context and action"><xsl:call-template name="link-context" /></xsl:when>
			</xsl:choose><xsl:text> </xsl:text>
			
			<xsl:element name="a">
				<xsl:attribute name="href"><xsl:value-of select="$DOMAIN" />/<xsl:value-of select="translate(@user,' ','+')" />/item/<xsl:value-of select="@id" /></xsl:attribute>
				<xsl:attribute name="class">etime utc-<xsl:value-of select="@timestamp" /></xsl:attribute>
				<xsl:attribute name="title"><xsl:value-of select="php:functionString('date','r',@timestamp)" /></xsl:attribute>
				<xsl:value-of select="php:functionString('date', 'D, j F Y - h:i a', @timestamp)" />
				<!--<xsl:call-template name="relative-time"><xsl:with-param name="diff"><xsl:value-of select="($TIMESTAMP - number(@timestamp))" /></xsl:with-param></xsl:call-template>-->
			</xsl:element>
			
			<xsl:if test="action and action/@id &lt; 50">
				<xsl:text> </xsl:text><span class="auto"><xsl:call-template name="autonomous" /></span>
			</xsl:if>
			
			<xsl:if test="false and context and action and context/@type!='user'">
			  <xsl:text> </xsl:text><xsl:call-template name="use-context" />
			</xsl:if>
			</div>

		</xsl:element>
	</xsl:template>
	
	<xsl:template match="event" mode="context">
    <xsl:element name="div">
    <xsl:attribute name="class">E <xsl:if test="action"> <xsl:value-of select="action/@type" /></xsl:if> e-<xsl:value-of select="@id" /></xsl:attribute>

      <div class="R">
        <xsl:call-template name="user-image"><xsl:with-param name="size" select="string(64)" /></xsl:call-template>
      </div>
      
      <div class="C">
      <xsl:if test="context and action and action/@type != 'reply'">
        <xsl:call-template name="action-context" />
		  </xsl:if>
		  <xsl:if test="message != ''">
        <p class="M"><xsl:value-of select="php:functionString('melative_link_message',message)" disable-output-escaping="yes" /></p>
      </xsl:if>
      </div>
      
      <div class="meta">
			<xsl:choose>
			  <xsl:when test="context and action and action/@type = 'reply'"><xsl:call-template name="link-context-reply" /></xsl:when>
			  <xsl:when test="context and action"><xsl:call-template name="link-context" /></xsl:when>
			</xsl:choose><xsl:text> </xsl:text>
			
			<xsl:element name="a">
				<xsl:attribute name="href"><xsl:value-of select="$DOMAIN" />/<xsl:value-of select="translate(@user,' ','+')" />/item/<xsl:value-of select="@id" /></xsl:attribute>
				<xsl:attribute name="class">etime utc-<xsl:value-of select="@timestamp" /></xsl:attribute>
				<xsl:attribute name="title"><xsl:value-of select="php:functionString('date','r',@timestamp)" /></xsl:attribute>
				<xsl:call-template name="relative-time"><xsl:with-param name="diff"><xsl:value-of select="($TIMESTAMP - number(@timestamp))" /></xsl:with-param></xsl:call-template>
			</xsl:element>
			
			<xsl:if test="action and action/@id &lt; 50">
				<xsl:text> </xsl:text><span class="auto"><xsl:call-template name="autonomous" /></span>
			</xsl:if>
			
			<xsl:if test="false and context and action and context/@type!='user'">
			  <xsl:text> </xsl:text><xsl:call-template name="use-context" />
			</xsl:if>
			</div>

		</xsl:element>
	</xsl:template>
	
	<xsl:template name="action-context">
	  <xsl:element name="p">
	    <xsl:attribute name="class">A</xsl:attribute>
	    <xsl:value-of select="action" disable-output-escaping="yes" /><xsl:if test="action/@data != ''"><xsl:text> </xsl:text><xsl:apply-templates select="action" mode="subcontext" /></xsl:if></xsl:element>
	</xsl:template>

	<xsl:template name="use-context">
	  <xsl:element name="a">
	    <xsl:attribute name="href">http://melative.com/stream?status=<xsl:value-of select="action/@type" /> /<xsl:value-of select="context/@type" />/<xsl:value-of select="context" />/<xsl:if test="action/@data"><xsl:value-of select="translate(action/@data,'|','/')" /></xsl:if>.<xsl:text> </xsl:text></xsl:attribute>
	    [use]
	  </xsl:element>
	</xsl:template>
		
	<xsl:template match="action" mode="subcontext">
	  <xsl:choose>
	    <xsl:when test="@id &gt;= 50 and @id &lt;300">
	      <small>(<xsl:value-of select="translate(@data,'|',' ')" />)</small>
	    </xsl:when>
	    <xsl:when test="@id = 15">
	      <small>(<xsl:value-of select="translate(substring-after(@data,'|'),'|',' ')" />)</small>
	    </xsl:when>
	    <xsl:when test="@id = 18">
	      <small><xsl:choose>
	        <xsl:when test="contains(@data,'|')">(<xsl:value-of select="substring-after(substring-after(@data,'|'),'|')" />) [<xsl:value-of select="substring-before(@data,'|')" />]</xsl:when>
	        <xsl:otherwise>(<xsl:value-of select="@data" />)</xsl:otherwise>
	      </xsl:choose></small>
	    </xsl:when>
	  </xsl:choose>
	</xsl:template>
	
	<xsl:template name="link-context">
		<xsl:element name="a">
  		<xsl:attribute name="href"><xsl:value-of select="$DOMAIN" />/<xsl:value-of select="translate(@user,' ','+')" />/stream/<xsl:value-of select="context/@type" />/<xsl:value-of select="translate(context, ' ', '+')" /></xsl:attribute>on <xsl:value-of select="context" />
		</xsl:element>
	</xsl:template>
	
	<xsl:template name="link-context-reply">
	  <xsl:choose>
	    <xsl:when test="action/@in_reply_to">in reply to <xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="$DOMAIN" />/<xsl:value-of select="translate(context,' ','+')" />/<xsl:value-of select="action/@in_reply_to" /></xsl:attribute><xsl:value-of select="context" />
					</xsl:element><xsl:text> </xsl:text>
				</xsl:when>
				<xsl:otherwise>in reply to <xsl:value-of select="context" /></xsl:otherwise>
				</xsl:choose>
	</xsl:template>
	
	<xsl:template name="autonomous">
		<xsl:choose>
			<xsl:when test="action/@type = 'reflection'">[<xsl:element name="a">
					<xsl:attribute name="href"><xsl:value-of select="$DOMAIN" />/user/<xsl:value-of select="translate(@user,' ','+')" />/reflections/<xsl:value-of select="substring-before(action/@data,'|')" /></xsl:attribute>reflection</xsl:element>]</xsl:when>
			<xsl:when test="action/@id = 16">[<xsl:element name="a">
					<xsl:attribute name="href"><xsl:value-of select="$DOMAIN" />/user/<xsl:value-of select="translate(@user,' ','+')" />/recommendations/<xsl:value-of select="context/@type" /></xsl:attribute>link</xsl:element>]</xsl:when>
			<xsl:when test="action/@id = 18">[<xsl:element name="a">
					<xsl:attribute name="href"><xsl:value-of select="$DOMAIN" />/<xsl:value-of select="translate(@user,' ','+')" />/associations/<xsl:value-of select="context/@type" />/<xsl:value-of select="translate(context, ' ', '+')" /></xsl:attribute>more</xsl:element>]</xsl:when>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>
